/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
  const { currentUser } = initialState ?? {};
  if (currentUser) {
    const my_menu: string[] = currentUser.authArray || [];
    const platform_menu: string[] = currentUser.current_platform.model_permissions || [];
    console.log('hhhhhh', currentUser.current_platform.model_permissions);
    const Secondary_permissions = (str1: string, str2: string) => {
      if (Array.isArray(my_menu))
        return (
          (my_menu?.includes(str1) || currentUser.is_master == 1) && platform_menu?.includes(str2)
        );
    };
    const First_level_permissions = (menuList: any, platformMenu: string) => {
      if (Array.isArray(my_menu)) {
        // 1.获取menuList的长度，获取me_menu的长度
        let Arr = [...menuList, ...my_menu]
        // 2.合并两个数组，并获取合并后的长度
        let conditionsArr = Array.from(
          new Set([...Arr].map((item: any) => JSON.stringify(item))),
        ).map((item: any) => JSON.parse(item));
        // 3.conditionsArr的长度和第一步获取的长度比较
        let flag = false
        if (((my_menu.length + menuList.length) > conditionsArr.length) || currentUser.is_master == 1) {
          flag = true
        }
        return (
          (flag || currentUser.is_master == 1) && platform_menu?.includes(platformMenu)
        );

      }
    };
    return {
      //欢迎
      // welcome: (route: any) => my_menu.includes('welcome'),
      // 患者管理(一级)
      patient: (route: any) => First_level_permissions(['allpatient', 'mypatient'], 'patient'),
      // 全部患者
      allpatient: (route: any) => Secondary_permissions('allpatient', 'patient'),
      // 我的患者
      mypatient: (route: any) => Secondary_permissions('mypatient', 'patient'),
      // 下属机构患者
      subordinate_patients: (route: any) => Secondary_permissions('subordinate_patients', 'patient'),
      // // 家庭病床（一级）
      familySickbeds: (route: any) => First_level_permissions(['establishedSickbeds', 'mySickbeds', 'bedApplication', 'callReminder'], 'familySickbeds'),
      // // 已建病床
      establishedSickbeds: (route: any) => Secondary_permissions('establishedSickbeds', 'familySickbeds'),
      // // 我负责的
      mySickbeds: (route: any) => Secondary_permissions('mySickbeds', 'familySickbeds'),
      // // 建床申请
      bedApplication: (route: any) => Secondary_permissions('bedApplication', 'familySickbeds'),
      // // 呼叫提醒
      callReminder: (route: any) => Secondary_permissions('callReminder', 'familySickbeds'),
      // 直播管理(一级)
      broadcast: (route: any) => First_level_permissions(['plan', 'manage', 'history'], 'broadcast'),
      // 直播计划
      plan: (route: any) => Secondary_permissions('plan', 'broadcast'),
      // 直播管理
      manage: (route: any) => Secondary_permissions('manage', 'broadcast'),
      // 历史直播
      history: (route: any) => Secondary_permissions('history', 'broadcast'),
      // 关联机构(一级)
      institution: () => First_level_permissions(['association', 'examine', 'unbind'], 'institution'),
      // 关联机构(二级)
      association: () => Secondary_permissions('association', 'institution'),
      // 机构关联审批
      examine: () => Secondary_permissions('examine', 'institution'),
      // 解绑记录
      unbind: () => Secondary_permissions('unbind', 'institution'),
      // 慢病管理（一级）
      chronic_diseases: () => First_level_permissions(['chronic_diseases'], 'chronic_diseases'),
      // 指导管理(一级)
      guide: () => First_level_permissions(['guide_plan', 'my_guide', 'guide_record'], 'guide'),
      // 指导计划
      guide_plan: () => Secondary_permissions('guide_plan', 'guide'),
      // 我的指导
      my_guide: () => Secondary_permissions('my_guide', 'guide'),
      // 指导记录
      guide_record: () => Secondary_permissions('guide_record', 'guide'),
      // 康复管理(一级)
      becovery_manage: () =>
        First_level_permissions(['becovery_assessment', 'assessment_record', 'video_manage'], 'becovery_manage'),
      // 康复评估
      becovery_assessment: () => Secondary_permissions('becovery_assessment', 'becovery_manage'),
      // 评估记录
      assessment_record: () => Secondary_permissions('assessment_record', 'becovery_manage'),
      // 视频管理
      video_manage: () => Secondary_permissions('video_manage', 'becovery_manage'),
      // 设备管理（一级）
      devices: () => First_level_permissions(['monitor_devices', 'monitoring_equipment'], 'devices'),
      // 监测设备
      monitor_devices: () => Secondary_permissions('monitor_devices', 'devices'),
      // 监护设备
      monitoring_equipment: () => Secondary_permissions('monitoring_equipment', 'devices'),
      // 系统设置(一级)
      system: (route: any) => First_level_permissions(['department', 'role', 'staff'], 'system'),
      //员工管理
      department: (route: any) => Secondary_permissions('department', 'system'),
      // 角色权限
      role: (route: any) => Secondary_permissions('role', 'system'),
      // 员工管理
      staff: (route: any) => Secondary_permissions('staff', 'system'),
    };
  } else {
    return {};
  }
}
